<?php


namespace models;


use think\Model;

class Category extends Model
{

    protected $categorys = array();

    public function check_catid($catid, $module_category)
    {
        if ($module_category == '*') return true;
        return in_array($catid, $module_category);
    }

    public function getCatTree($arr, $id = 0, $lev = 0)
    {
        $tree = array();
        foreach ($arr as $v) {
            if ($v['parentid'] == $id) {
                $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree, $this->getCatTree($arr, $v['catid'], $lev + 1));
            }
        }
        return $tree;
    }

    public function category_select($moduleid)
    {
        $all_cats = \think\facade\Db::name('category')->where('moduleid', $moduleid)->order('listorder asc')->select()->toArray();
        return $this->getCatTree($all_cats);
    }

    protected function get_arrparentid($catid, $arrparentid = '', $n = 1)
    {
        if ($n > 10 || !is_array($this->categorys) || !isset($this->categorys[$catid])) return false;
        $parentid = $this->categorys[$catid]['parentid'];
        $arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
        if ($parentid) {
            $arrparentid = $this->get_arrparentid($parentid, $arrparentid, ++$n);
        } else {
            $this->categorys[$catid]['arrparentid'] = $arrparentid;
        }
        return $arrparentid;
    }

    protected function get_arrchildid($catid)
    {
        $arrchildid = $catid;
        if (is_array($this->categorys)) {
            foreach ($this->categorys as $id => $cat) {
                if ($cat['parentid'] && $id != $catid && $cat['parentid'] == $catid) {
                    $arrchildid .= ',' . $this->get_arrchildid($id);
                }
            }
        }
        return $arrchildid;
    }

    public function cache($moduleid)
    {

        $this->categorys = \think\facade\Db::name('category')->where(array('moduleid' => $moduleid))->order('listorder asc')->column('*', 'catid');

        foreach ($this->categorys as $catid => $cat) {
            $arrparentid = $this->get_arrparentid($catid);
            $arrchildid = $this->get_arrchildid($catid);
            $child = is_numeric($arrchildid) ? 0 : 1;
            $tmp = array();
            $tmp = array_merge($tmp, $cat);
            $tmp['arrparentid'] = $arrparentid;
            $tmp['arrchildid'] = $arrchildid;
            $tmp['child'] = $child;
            $tmp['cat_pos'] = $this->cat_pos($tmp);

            \think\facade\Db::name('category')->field('arrparentid,arrchildid,child,cat_pos')->where('catid', $catid)->update($tmp);
        }

        return true;
    }

    public function get_category($catid)
    {
        $catid = intval($catid);
        return \think\facade\Db::name('category')->where(array('catid' => $catid))->find();
    }

    public function cat_pos($CAT, $str = ' &raquo; ')
    {
        if (!$CAT) return '';

        if (!isset($CAT['arrparentid']) || !$CAT['arrparentid']) {
            return $CAT['catname'];
        }

        $arrparentids = $CAT['arrparentid'] . ',' . $CAT['catid'];
        $arrparentid = explode(',', $arrparentids);
        $pos = '';
        $CATEGORY = array();
        $ddd = \think\facade\Db::name('category')->where(" catid IN ($arrparentids) ")->select()->toArray();

        foreach ($ddd as $r) {
            $CATEGORY[$r['catid']] = $r;
        }
        $last = count($arrparentid) - 1;
        foreach ($arrparentid as $index => $catid) {
            if (!$catid || !isset($CATEGORY[$catid])) continue;
            if ($index == $last) {
                $pos .= $CATEGORY[$catid]['catname'] . $str;
            } else {
                $pos .= $CATEGORY[$catid]['catname'] . $str;
            }
        }
        $_len = strlen($str);
        if ($str && substr($pos, -$_len, $_len) === $str) $pos = substr($pos, 0, strlen($pos) - $_len);
        return $pos;
    }

    public function max_catid()
    {
        $catid = \think\facade\Db::name('category')->max('catid');
        return intval($catid);
    }


    public function mod_check_ok($moduleid, $catid, $parentid)
    {
        $all_cats = \think\facade\Db::name('category')->where(array('moduleid' => $moduleid))->order('listorder asc')->select()->toArray();
        $sub_cats = $this->getCatTree($all_cats, $catid);
        $ids = array();
        foreach ($sub_cats as $v) {
            $ids[] = $v['catid'];
        }
        #判断所选的父分类是否为当前分类或其后代分类
        if ($parentid == $catid || in_array($parentid, $ids)) return false;
        return true;
    }

    public function del_check_ok($catid, $moduleid)
    {
        $r = \think\facade\Db::name('category')->field('catid')->where(array('parentid' => $catid))->find();
        if ($r) return false;

        $menu = \Menu::get($moduleid);
        $r = \think\facade\Db::name($menu['module_en'])->where(array('catid' => $catid))->count();
        if ($r) return false;

        return true;
    }

    //处理编辑时的垃圾附件
    public function treat_attachment($pk)
    {
        $data = \think\facade\Db::name('category')->where(['catid' => $pk])->find();
        $string = $data['thumb'];

        $attachments = \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->select()->toArray();
        foreach ($attachments as $k => $v) {
            if (strpos($string, $v['url']) !== false) continue;

            $file = root_path() . 'public' . $v['url'];
            if (!file_exists($file)) continue;
            unlink($file);
            \think\facade\Db::name('category_attachment')->where(['catid' => $v['catid']])->delete();
        }
        return true;
    }

    //删除附件
    public function clear_attachment($pk)
    {
        $attachments = \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->select()->toArray();
        foreach ($attachments as $k => $v) {
            $file = root_path() . 'public' . $v['url'];
            if (!file_exists($file)) continue;
            unlink($file);
        }
        \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->delete();
    }

}